package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntCdbgmdBaDetailBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntCdbgmdBaDetailVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntCdbgmdBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntCdbgmdBaDetailMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntCdbgmdBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土坍落度、观密度试验观密度详情Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntCdbgmdBaDetailServiceImpl implements IReportSnhntCdbgmdBaDetailService {

    private final ReportSnhntCdbgmdBaDetailMapper baseMapper;

    /**
     * 查询水泥混凝土坍落度、观密度试验观密度详情
     */
    @Override
    public ReportSnhntCdbgmdBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水泥混凝土坍落度、观密度试验观密度详情列表
     */
    @Override
    public TableDataInfo<ReportSnhntCdbgmdBaDetailVo> queryPageList(ReportSnhntCdbgmdBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntCdbgmdBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntCdbgmdBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土坍落度、观密度试验观密度详情列表
     */
    @Override
    public List<ReportSnhntCdbgmdBaDetailVo> queryList(ReportSnhntCdbgmdBaDetailBo bo) {
        LambdaQueryWrapper<ReportSnhntCdbgmdBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntCdbgmdBaDetail> buildQueryWrapper(ReportSnhntCdbgmdBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntCdbgmdBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSnhntCdbgmdBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShijbh()), ReportSnhntCdbgmdBaDetail::getShijbh, bo.getShijbh());
        lqw.eq(bo.getShiytzl1() != null, ReportSnhntCdbgmdBaDetail::getShiytzl1, bo.getShiytzl1());
        lqw.eq(bo.getShiytzl2() != null, ReportSnhntCdbgmdBaDetail::getShiytzl2, bo.getShiytzl2());
        lqw.eq(bo.getHunnthsytzzl1() != null, ReportSnhntCdbgmdBaDetail::getHunnthsytzzl1, bo.getHunnthsytzzl1());
        lqw.eq(bo.getHunnthsytzzl2() != null, ReportSnhntCdbgmdBaDetail::getHunnthsytzzl2, bo.getHunnthsytzzl2());
        lqw.eq(bo.getShiyzl1() != null, ReportSnhntCdbgmdBaDetail::getShiyzl1, bo.getShiyzl1());
        lqw.eq(bo.getShiyzl2() != null, ReportSnhntCdbgmdBaDetail::getShiyzl2, bo.getShiyzl2());
        lqw.eq(bo.getShiytrj1() != null, ReportSnhntCdbgmdBaDetail::getShiytrj1, bo.getShiytrj1());
        lqw.eq(bo.getShiytrj2() != null, ReportSnhntCdbgmdBaDetail::getShiytrj2, bo.getShiytrj2());
        lqw.eq(bo.getBiaogmd1() != null, ReportSnhntCdbgmdBaDetail::getBiaogmd1, bo.getBiaogmd1());
        lqw.eq(bo.getBiaogmd2() != null, ReportSnhntCdbgmdBaDetail::getBiaogmd2, bo.getBiaogmd2());
        lqw.eq(bo.getBiaogmdpjz() != null, ReportSnhntCdbgmdBaDetail::getBiaogmdpjz, bo.getBiaogmdpjz());
        return lqw;
    }

    /**
     * 新增水泥混凝土坍落度、观密度试验观密度详情
     */
    @Override
    public Boolean insertByBo(ReportSnhntCdbgmdBaDetailBo bo) {
        ReportSnhntCdbgmdBaDetail add = BeanUtil.toBean(bo, ReportSnhntCdbgmdBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水泥混凝土坍落度、观密度试验观密度详情
     */
    @Override
    public Boolean updateByBo(ReportSnhntCdbgmdBaDetailBo bo) {
        ReportSnhntCdbgmdBaDetail update = BeanUtil.toBean(bo, ReportSnhntCdbgmdBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntCdbgmdBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土坍落度、观密度试验观密度详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
